//
//  StringSizeEngine.swift
//  home_ios
//
//  Created by humin on 2016/12/13.
//  Copyright © 2016年 yihua. All rights reserved.
//

import UIKit

class StringSizeEngine: NSObject {
    
    class func calculateStringSize(_ str:String, size:CGSize, font:UIFont) -> CGSize{
        var stringSize = CGSize.init(width: 0, height: 0)
        if str.characters.count > 0{
            let attrs = [NSFontAttributeName : font]
            let size = str.boundingRect(with: size, options:.usesLineFragmentOrigin, attributes: attrs, context: nil)
            let nw = ceil(size.width)
            let nh = ceil(size.height)
            
            stringSize = CGSize.init(width: nw, height: nh)
        }
        return stringSize
    }
    
    class func calculateStringWidth(_ str:String, height: CGFloat, font: UIFont) -> CGFloat{
        let size = CGSize.init(width: AppWidth, height: height)
        return StringSizeEngine.calculateStringSize(str, size: size, font: font).width
    }

    class func calculateStringHeight(_ str:String, width: CGFloat, font: UIFont) -> CGFloat{
        let size = CGSize.init(width: width, height: 100000)
        return StringSizeEngine.calculateStringSize(str, size: size, font: font).height
    }

}
